# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from .settings import MYSQL_CONFIG

class MaoyanPipeline:
    def process_item(self, item, spider):
        print(item['_name'], item['_actors'], item['_time'])
        return item


class MaoyanMysqlPipeline:

    def open_spider(self,spider):
        print('我是open spider')
        self.db=pymysql.connect(**MYSQL_CONFIG)
        self.cursor=self.db.cursor()
        table_create='create table if not exists films(name varchar(200),actors varchar(200),time datetime)'
        self.cursor.execute(table_create)




    def process_item(self, item, spider):
        add_list=[
            item['_name']
            ,item['_actors']
            ,item['_time']
        ]
        sql='insert into films(name,actors,time) values(%s,%s,%s)'
        self.cursor.execute(sql,add_list)
        self.db.commit()

        return item


    def close_spider(self, spider):
        print('我是close spider')
        self.cursor.close()
        self.db.close()